#!/bin/bash

# ----------------------------------------------------------------------
# Filename:   smbstatus.sh
# Version:    1.0
# Date:       2013/11/20
# Author:     huijing.hei
# Email:      huijing.hei@cs2c.com.cn
# Summary:    02系统基本功能-04网络服务-04Samba-06查看samba连接
# Notes:       ***
# Copyright:    China Standard Software Co., Ltd.
# History：     
#             Version 1.0, 2013/11/20
#             - smb: smbstatus.
# ----------------------------------------------------------------------

##! @TODO: setup
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
##! @OUT: 0 => success; 1 => failure

function do_setup()
{
    trap do_cleanup INT EXIT
    serv=smb

    SMB_LABEL=test1

    CONF=/etc/samba/smb.conf

    exists smbstatus
    EchoResult "check commands"

    # add user on rhost
    TEST_USER="smbuser"
    TEST_USER_PASSWD="1"

    # add user
    ${RSH} -n -l root $RHOST "/usr/sbin/userdel -r ${TEST_USER}; /usr/sbin/useradd  ${TEST_USER}"

    # add user to smb
    ${RSH} -n -l root $RHOST "echo -ne \"${TEST_USER_PASSWD}\n${TEST_USER_PASSWD}\n\" | /usr/bin/smbpasswd -a ${TEST_USER}"

    # check smbuser
    RE=`${RSH} -n -l root $RHOST "/usr/bin/pdbedit -L ${TEST_USER}"`
    echo ${RE} | grep ${TEST_USER}
    EchoResult "added user ${TEST_USER}"


    # server mkdir
    TMP_DIR=/tmp/smbshare
    ${RSH} -l root -n $RHOST "cp ${CONF} ${CONF}.bak; \
                              [ -d ${TMP_DIR} ] || /bin/mkdir ${TMP_DIR} && chmod 777 ${TMP_DIR}"

    # smb.conf
    t1="
[${SMB_LABEL}]
path = ${TMP_DIR}
read only = no
browseable = yes
valid users = ${TEST_USER}
"

    ${RSH} -l root -n $RHOST "[ -f ${CONF} ] && grep \"workgroup = MYGROUP\" ${CONF} && grep \"^[[:space:]]*security = user\" ${CONF}; \
                              echo \"${t1}\" >> ${CONF} "

    sleep 5
    # restart smb server
    ${RSH} -l root -n $RHOST "command -v systemctl && /bin/systemctl restart ${serv}.service || /sbin/service ${serv} restart"

    sleep 3

    RE=`${RSH} -l root -n $RHOST "netstat -ntlp | grep ${serv}"`
    echo ${RE} | grep ${serv}
    EchoResult "${serv} start on $RHOST"

    # mount dir
    MOUNT_DIR=/tmp/smbmount
    [ -d ${MOUNT_DIR} ] || mkdir ${MOUNT_DIR} && chmod 777 ${MOUNT_DIR}
    EchoResult "mkdir ${MOUNT_DIR}"

    mount -t cifs -o user=${TEST_USER},password=${TEST_USER_PASSWD} //${RHOST}/${SMB_LABEL} ${MOUNT_DIR}

    # test
    mount | grep ${MOUNT_DIR} && echo "test" > ${MOUNT_DIR}/test
    EchoResult "mount smb user with correct password"




}

##! @TODO: test: correct username and password
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
##! @OUT: 0 => success; 1 => failure

function do_test()
{
    ip=`ifconfig | grep 'inet ' | grep -m 1 -v '127.0.0.1'  | awk '{print $2}' | tr -d "addr:"`
    sleep 5   

    # smbstatus
    RE=`${RSH} -l root -n $RHOST "/usr/bin/smbstatus"`
    echo "${RE}" | grep ${ip}
    EchoResult "smbstatus"


}

##! @TODO: cleanup
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
##! @OUT: 0 => success; 1 => failure

function do_cleanup()
{
    umount ${MOUNT_DIR}
 
    ${RSH} -l root -n $RHOST "
           command -v systemctl && /bin/systemctl stop ${serv}.service || /sbin/service ${serv} stop; \
           rm -rf ${TMP_DIR}; \
           mv -f ${CONF}.bak ${CONF}; \
           /usr/bin/smbpasswd -x ${TEST_USER};\
           /usr/sbin/userdel -r ${TEST_USER}"

    rm -rf ${MOUNT_DIR}

}

# include lib files

if [ -z "$SFROOT" ]
then
    CMDLIB=./
    SFROOT=./
else
    CMDLIB=${SFROOT}/testcases/Network_Service
fi

. ${SFROOT}/lib/Echo.sh
. ${CMDLIB}/lib/Net.sh
. ${SFROOT}/lib/XmlParse.sh

. ${CMDLIB}/setup.sh

# get RHOST, RUSERNAME, PASSWORD from config.xml
DATA_FILE="${SFROOT}/testcases/Network_Service/config.xml"
[ -e $DATA_FILE ] || { echo "$DATA_FILE not find, test end."; exit 1; }

XmlParse ${DATA_FILE}

GetValue REHOST ReHost
GetValue RUSERNAME UserName
GetValue PASSWORD RootPasswd

export RHOST=${REHOST}
export RUSERNAME=${RUSERNAME}
export PASSWORD=${PASSWORD}

setup_env
EchoResult "setup env"

do_setup
do_test
EchoResult "testcase : ${serv} smbstatus"
